import config from "../../config";
import {pageIndex} from "../../pages";

export default {
  title: '角色列表',
  type: 'table',
  permission: '/system/role/list',
  config: {
    dataUrl: '/system/role/list',

    filter: {
      controls: [
        {
          key: 'nameIs',
          label: '名称',
          type: 'text'
        }
      ]
    },

    columns: [
      {
        key: 'createdDate',
        title: '创建时间',
        type: 'datetime',
        sorter: true,
        defaultSortOrder: 'descend'
      },
      {
        key: 'name',
        title: '名称'
      },
      {
        key: 'description',
        title: '描述'
      },
    ],

    actions: [
      {
        key: 'create',
        text: '添加角色',
        icon: 'plus',
        type: 'form',
        permission: '/system/role/create',
        config: {
          color: 'primary',
          title: '添加角色',
          submitUrl: '/system/role/create',
          submitText: '确认添加',
          submitSuccessMessage: '添加成功',
          controls: [
            {
              key: 'name',
              label: '角色名',
              type: 'text',
              required: true,
              config: {
                rules: [
                  {max: 30, message: '最多输入 30 字符'}
                ]
              }
            },
            {
              key: 'description',
              label: '描述',
              type: 'textarea',
              config: {
                rules: [
                  {max: 200, message: '只能输入 200 个字符'}
                ]
              }
            },
          ]
        }
      }
    ],

    rowActions: [
      {
        key: 'edit',
        text: '修改',
        type: 'form',
        icon: 'edit',
        permission: '/system/role/edit',
        config: {
          color: 'primary',
          title: '修改角色',
          initUrl: '/system/role/edit/get',
          submitUrl: '/system/role/edit',
          submitText: '确认修改',
          submitSuccessMessage: '修改成功',
          controls: [
            {
              key: 'name',
              label: '角色名',
              type: 'text',
              required: true,
              config: {
                rules: [
                  {min: 1, max: 30, message: '只能输入1-30个字符'}
                ]
              }
            },
            {
              key: 'description',
              label: '描述',
              type: 'textarea',
              config: {
                rules: [
                  {min: 1, max: 500, message: '只能输入1-500个字符'}
                ]
              }
            },
          ]
        }
      },
      {
        key: 'grantPermissions',
        text: '分配权限',
        icon: 'lock',
        type: 'form',
        permission: '/system/role/grant',
        config: {
          color: 'primary',
          title: '分配权限',
          initUrl: '/system/role/grant/get',
          submitUrl: '/system/role/grant',
          submitText: '确认分配',
          submitSuccessMessage: '分配成功',
          controls: [
            {
              key: 'permissions',
              label: '权限',
              type: 'tree',
              config: {
                options() {
                  return config.menus.filter(menu => menu.pages?.length > 0 || menu.permission != null).map(menu => ({
                    key: menu.key,
                    title: menu.text,
                    icon: menu.icon,
                    children: menu.pages?.map(pageKey => {
                      const page = pageIndex[pageKey];
                      return {
                        key: page.key,
                        title: page.title,
                        icon: page.icon,
                        scopedSlots: {icon: 'icon'},
                        children: [
                          {
                            key: page.permission,
                            title: '查看列表'
                          },
                          ...(page.config.actions ?? []).map(action => ({
                            title: action.text,
                            icon: action.icon,
                            scopedSlots: {icon: 'icon'},
                            key: action.permission
                          })),
                          ...(page.config.rowActions ?? []).flatMap(action => (Array.isArray(action) ? action : [action]).map(action => ({
                            title: action.text,
                            icon: action.icon,
                            scopedSlots: {icon: 'icon'},
                            key: action.permission
                          }))),
                          ...(page.config.export != null ? [{
                            title: '导出',
                            key: page.config.export.permission
                          }] : [])
                        ]
                      }
                    })
                  }))
                }
              }
            }
          ]
        }
      },
    ]
  }
}

